//
//  UIImage+DP.m
//  Rail
//
//  Created by hychen on 2018/3/4.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "UIImage+DP.h"

@implementation UIImage (DP)
#pragma mark 可以拉伸的图片
+ (UIImage *)resizedImage:(NSString *)imgName
{
    return [self resizedImage:imgName xPos:0.5 yPos:0.5];
}

+ (UIImage *)resizedImage:(NSString *)imgName xPos:(CGFloat)xPos yPos:(CGFloat)yPos
{
    UIImage *image = [UIImage imageNamed:imgName];
    CGFloat width = image.size.width * xPos;
    return [image stretchableImageWithLeftCapWidth:width topCapHeight:image.size.height * yPos];
}

// 获取上传图片（压缩过）
- (UIImage *)getUploadImage
{
    float img_w = self.size.width;
    float img_h = self.size.height;
    UIImage *uploadImage = self;
    if (self.size.width > 375 || self.size.height > 667) {
        if (self.size.width>self.size.height) {
            img_w = 375;
            img_h = self.size.width / self.size.height * img_w;
        }else{
            img_h = 667;
            img_w = self.size.width / self.size.height * img_h;
        }
        
        CGSize targetSize = [self getScaleRect:self.size targetSize:CGSizeMake(img_w, img_h)];
        uploadImage = [self resizedImage:self width:targetSize.width height:targetSize.height];
    }
    NSData *imageData = UIImageJPEGRepresentation(uploadImage, 0.5);
    uploadImage = [UIImage imageWithData:imageData];
    
    return uploadImage;
}

- (UIImage *)zipImage {
    //进行图像尺寸的压缩
    CGSize imageSize = self.size;//取出要压缩的image尺寸
    CGFloat width = imageSize.width;    //图片宽度
    CGFloat height = imageSize.height;  //图片高度
    //1.宽高大于1280(宽高比不按照2来算，按照1来算)
    if (width>640||height>640) {
        if (width>height) {
            CGFloat scale = height/width;
            width = 640;
            height = width*scale;
        }else{
            CGFloat scale = width/height;
            height = 640;
            width = height*scale;
        }
        //2.宽大于1280高小于1280
    }else if(width>640||height<640){
        CGFloat scale = height/width;
        width = 640;
        height = width*scale;
        //3.宽小于1280高大于1280
    }else if(width<640||height>640){
        CGFloat scale = width/height;
        height = 640;
        width = height*scale;
        //4.宽高都小于1280
    }else{
    }
    
    UIGraphicsBeginImageContext(CGSizeMake(width, height));
    [self drawInRect:CGRectMake(0,0,width,height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    //进行图像的画面质量压缩
    NSData *data=UIImageJPEGRepresentation(newImage, 0.7);
    if (data.length>1024*1024) {//1M以及以上
        data=UIImageJPEGRepresentation(newImage, 0.4);
    }else if (data.length>512*1024) {//0.5M-1M
        data=UIImageJPEGRepresentation(newImage, 0.5);
    }else if (data.length>200*1024) {
        //0.25M-0.5M
        data=UIImageJPEGRepresentation(newImage, 0.6);
    }
    return [UIImage imageWithData:data];;
}

- (CGSize)getScaleRect:(CGSize)originSize targetSize:(CGSize)maxSize{
    CGFloat width = originSize.width;
    CGFloat height = originSize.height;
    
    CGFloat wSize = width/maxSize.width;
    CGFloat hSize = height/maxSize.height;
    
    //    CGFloat currSize = wSize < hSize ? wSize : hSize;
    
    //Loong modified
    CGFloat currSize = wSize > hSize ? wSize : hSize;
    
    CGFloat finelWidth = width/currSize;
    CGFloat finelHeight = height/currSize;
    
    return CGSizeMake(finelWidth, finelHeight);
}

- (UIImage *)resizedImage:(UIImage *)inImage width:(NSInteger)a height:(NSInteger)b
{
    float actualHeight = inImage.size.height;
    float actualWidth = inImage.size.width;
    float imgRatio = actualWidth/actualHeight;
    float maxRatio = 320.0/480.0;
    
    if(imgRatio!=maxRatio){
        if(imgRatio < maxRatio){
            imgRatio = 480.0 / actualHeight;
            actualWidth = imgRatio * actualWidth;
            actualHeight = 480.0;
        }
        else{
            imgRatio = 320.0 / actualWidth;
            actualHeight = imgRatio * actualHeight;
            actualWidth = 320.0;
        }
    }
    CGRect rect = CGRectMake(0.0, 0.0, a, b);
    UIGraphicsBeginImageContext(rect.size);
    [inImage drawInRect:rect];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

+ (UIImage *)resizedImage:(UIImage *)inImage width:(NSInteger)w height:(NSInteger)h {
    float actualHeight = inImage.size.height;
    float actualWidth = inImage.size.width;
    float imgRatio = actualWidth/actualHeight;
    float maxRatio = 320.0/480.0;
    
    if(imgRatio!=maxRatio){
        if(imgRatio < maxRatio){
            imgRatio = 480.0 / actualHeight;
            actualWidth = imgRatio * actualWidth;
            actualHeight = 480.0;
        }
        else{
            imgRatio = 320.0 / actualWidth;
            actualHeight = imgRatio * actualHeight;
            actualWidth = 320.0;
        }
    }
    CGRect rect = CGRectMake(0.0, 0.0, w, h);
    UIGraphicsBeginImageContext(rect.size);
    [inImage drawInRect:rect];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}
@end
